package org.alvin;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ListObjectsV2Result;
import com.amazonaws.services.s3.model.S3ObjectSummary;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

public class S3Demo {

    public static void main(String[] args) {
        getS3Object("qds-tst");
    }

    public static AmazonS3 amazonS36() {
//        AWSCredentials credentials = new BasicAWSCredentials("6DUJ5YLI0RTHUKJF7JCG", "8zlRohXxvtcDKwBBCdWXYG37h6lnFfwyqszkO2tP");
//        ClientConfiguration clientConfig = new ClientConfiguration();
//        clientConfig.setProtocol(Protocol.HTTP);
////        clientConfig.withSignerOverride("S3SignerType");
//        //noinspection deprecation
//        AmazonS3 s3 = new AmazonS3Client(credentials, clientConfig);
//        s3.setEndpoint("http://oss.xcloud.lenovo.com:8084");
//        s3.setRegion(Region.getRegion(Regions.fromName("us-east-1")));
//        return s3;
        AWSCredentials credentials = new BasicAWSCredentials("6DUJ5YLI0RTHUKJF7JCG", "8zlRohXxvtcDKwBBCdWXYG37h6lnFfwyqszkO2tP");
        ClientConfiguration configuration = new ClientConfiguration();
        configuration.setProtocol(Protocol.HTTP);
        configuration.setSignerOverride("S3SignerType");
        AmazonS3ClientBuilder builder = AmazonS3Client.builder().enablePathStyleAccess();
        builder.setClientConfiguration(configuration);
        builder.setCredentials(new AWSStaticCredentialsProvider(credentials));
        builder.setEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration("http://oss.xcloud.lenovo.com:8084", Regions.CN_NORTH_1.getName()));
      return builder.build();
    }

    /**
     * 获取s3桶中数据
     */
    public static void getS3Object(String bucketName) {
        AmazonS3 amazonS3 = amazonS36();
        ListObjectsV2Result result = amazonS3.listObjectsV2(bucketName);
        List<S3ObjectSummary> objects = result.getObjectSummaries();
        for (S3ObjectSummary os : objects) {
            System.out.println("* " + os.getKey());
        }
    }

    /**
     * 上传文件
     *
     * @param filePath   本地文件路径
     * @param bucketName 桶名称
     * @param keyName    桶路径以及名称  实例："assets/test/1513132153/1.png";
     * @throws IOException
     */
    private static void uploadFileToBucket(String filePath, String bucketName, String keyName) throws IOException {
//        AmazonS3 s3Client = amazonS36();
//        File imageFile = new File(filePath);
//        FileInputStream in = new FileInputStream(imageFile);
//        MultipartFile file = new MockMultipartFile("file", imageFile.getName(), "text/plain", IOUtils.toByteArray
//                (in));
//        ObjectMetadata metadata = new ObjectMetadata();
//        metadata.setContentType(file.getContentType());
//        metadata.setContentLength(file.getSize());
//        try {
//            s3Client.putObject(new PutObjectRequest(
//                    bucketName, keyName, file.getInputStream(), metadata));
//        } catch (AmazonServiceException ase) {
//            ase.getMessage();
//        }
    }
}
